Next: 3.8 Overlaying Up: 3. Using SDCC Previous: 3.6 Absolute Addressing Contents Index

#
3.7 Parameters & Local Variables

Automatic (local) variables and parameters to functions can either be placed on the stack or in data-space. The default action of the compiler is to place these variables in the internal RAM (for small model) or external RAM (for medium or large model). This in fact makes them similar to static so by default functions are non-reentrant.
They can be placed on the stack by using the --stack-auto option, by using #pragma stackauto or by using the reentrant keyword in the function declaration, e.g.:

> `unsigned char foo(char i) __reentrant`
> `{`
> `...`
> `}`

Since stack space on 8051 is limited, the reentrant keyword or the --stack-auto option should be used sparingly. Note that the reentrant keyword just means that the parameters & local variables will be allocated to the stack, it does not mean that the function is register bank independent.
Local variables can be assigned storage classes and absolute addresses, e.g.:

> `unsigned char foo(__xdata int parm)`
> `{`
> `__xdata unsigned char i;`
> `__bit bvar;`
> `__data __at (0x31) unsigned char j;`
> `...`
> `}`

In the above example the parameter parm and the variable i will be allocated in the external ram, bvar in bit addressable space and j in internal ram. When compiled with --stack-auto or when a function is declared as reentrant this should only be done for static variables.

It is however allowed to use bit parameters in reentrant functions and also non-static local bit variables are supported. Efficient use is limited to 8 semi-bitregisters in bit space. They are pushed and popped to stack as a single byte just like the normal registers.

Next: 3.8 Overlaying Up: 3. Using SDCC Previous: 3.6 Absolute Addressing Contents Index